package logic

import (
	"context"
	"fmt"
	"lucky/models"
	"lucky/pkg"

	"lucky/luckyrpc/internal/svc"
	"lucky/luckyrpc/luckyrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatPayLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreatPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatPayLogic {
	return &CreatPayLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreatPayLogic) CreatPay(in *luckyrpc.CreatPayRequest) (*luckyrpc.CreatPayResponse, error) {
	// todo: add your logic here and delete this line

	var order models.Order
	err := models.DB.Debug().Where("order_sn = ?", in.Sn).First(&order).Error
	if order.ID == 0 {
		return nil, fmt.Errorf("当前订单不存在")
	}

	url, err := pkg.AliPay(order.OrderSn, order.Total)
	if err != nil {
		return nil, err
	}

	return &luckyrpc.CreatPayResponse{
		Url: url,
	}, nil
}
